import json
import requests
import time

usertoken = 'Your_token_from_wx'

database = {}

def get_timestamp():
    return int(round(time.time() * 1000))


def send_request(url, para:dict={}):
    url = 'https://gateway.vocabgo.com/Student' + url
    para['timestamp'] = get_timestamp()
    headers = {
        'usertoken':usertoken
    }
    respond = requests.get(url, params=para, headers=headers).text
    return json.loads(respond)
    
def get_data():
    database = {}
    main_menu = send_request('/Course/List')
    try:
        for book in main_menu['data']['course_info_list']:
            book_menu = send_request('/StudyTask/List', {'course_id':book['course_id']})
            print("Fucking:" + book['course_id'])
            mem = set()
            for llist in book_menu['data']['task_list']:
                if llist['list_id'] in mem:
                    continue
                mem.add(llist['list_id'])
                print("--------Fucking:" + llist['list_id'])
                word_list = send_request('/StudyTask/Info?task_id=-1', 
                {'course_id':book['course_id'], 'list_id':llist['list_id']})
                for word in word_list['data']['word_list']:
                    word_info = send_request('/Course/StudyWordInfo', 
                    {'course_id':book['course_id'], 'list_id':llist['list_id'], 'word':word['word']})
                    print('----------------Fucking:' + word['word'])
                    if word['word'] in database:
                        database[word['word']].extend(word_info['data']['options'])
                    else:
                        database[word['word']] = word_info['data']['options']
    finally:
        with open('database.json', 'w') as f:
            f.write(json.dumps(database))
        pass
